package com.shotnshare.app;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v4.widget.SlidingPaneLayout.LayoutParams;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class ImageScreen extends SherlockActivity{

	ImageView choosenImage;
	
	private ArrayList<String> burstmode_images = new ArrayList<String>();
	private Bundle extras;
	private int selected_image_index;
	private Gallery gallery;
	
	private void Load_Ads() {
		
		AdView adView=null;
		if(adView == null) {
			adView = (AdView)this.findViewById(R.id.adsImage);
        }
		AdRequest ar = new AdRequest();
	 /*   ar.addTestDevice(AdRequest.TEST_EMULATOR);
	    ar.addTestDevice("YT9101D530"); //Sony
	    ar.addTestDevice("HT265W507354");	
	    ar.addTestDevice("SH35JVV00526"); //Tri
	    ar.addTestDevice("323150C41B6900EC"); //Client
	    ar.addTestDevice("c1607d41105938f"); // My*/
	    adView.loadAd(ar);
	    
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choose_img);
		
		//Action bar: add Back button
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		Load_Ads();
		
		// Get Extras
		extras = getIntent().getExtras();
		
		// Get Image list
		burstmode_images = extras.getStringArrayList("image_list");
		
		int center_element_index = burstmode_images.size()/2;
		selected_image_index = center_element_index;
		
		final ImageView choosenImage=(ImageView)findViewById(R.id.choosen_image);
		Bitmap myBitmap = BitmapFactory.decodeFile(burstmode_images.get(center_element_index));
		choosenImage.setImageBitmap(myBitmap);
		
		
		this.gallery=(Gallery)findViewById(R.id.gallery_imgs);
		  
		this.gallery.setAdapter(new ImageAdapter(this));
		//this.gallery.setSelection(center_element_index);
		
		DisplayMetrics metrics = new DisplayMetrics();
		WindowManager wm=(WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
		wm.getDefaultDisplay().getMetrics(metrics);
		
		/*if (tmp < gallery.getWidth() )
		{
		  MarginLayoutParams mlp = (MarginLayoutParams) gallery.getLayoutParams();
		  mlp.setMargins(-((metrics.widthPixels/2)+100), 
		                 mlp.topMargin, 
		                 mlp.rightMargin, 
		                 mlp.bottomMargin
		  );
		} */
		//gallery.setGravity(0);
		//gallery.setSpacing(0);
		
		Display display = getWindowManager().getDefaultDisplay();
		int width = display.getWidth();
		//Log.d("Width", getString(gallery.getWidth()));
		//Log.d("Width", getString(width));
		MarginLayoutParams mlp = (MarginLayoutParams) gallery.getLayoutParams();
		mlp.setMargins(-((metrics.widthPixels/2)+60), 
			                 mlp.topMargin, 
			                 mlp.rightMargin, 
			                 mlp.bottomMargin
			  );		
		
			
	
				
		this.gallery.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Bitmap myBitmap = BitmapFactory.decodeFile(burstmode_images.get(arg2));
				choosenImage.setImageBitmap(myBitmap);
				ImageScreen.this.selected_image_index = arg2;
			}
		});
	}
	
	public class ImageAdapter extends BaseAdapter{

		private Context ctx;
    	int imageBackground;
    	
    	public ImageAdapter(Context c) {
			ctx = c;
			TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
			imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
			ta.recycle();
		}
    	
    	@Override
    	public int getCount() {
    		return burstmode_images.size();
    	}

    	@Override
    	public Object getItem(int arg0) {
    		
    		return arg0;
    	}

    	@Override
    	public long getItemId(int arg0) {
    		
    		return arg0;
    	}

    	@Override
    	public View getView(int arg0, View arg1, ViewGroup arg2) {
    		Bitmap myBitmap = BitmapFactory.decodeFile(burstmode_images.get(arg0));
    		
    		
    		Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    		int width = display.getWidth(); // to get width of the screen
    		int height = display.getHeight();
    		 
    		ImageView iv = new ImageView(ctx);
    		iv.setImageBitmap(myBitmap);
    		iv.setScaleType(ImageView.ScaleType.FIT_XY);
    		
    		if(isTabletDevice(ctx))
    			iv.setLayoutParams(new Gallery.LayoutParams(height/5,height/5));
    		else
    			iv.setLayoutParams(new Gallery.LayoutParams(height/4,height/4));
    		
    		iv.setBackgroundResource(imageBackground);
    		return iv;
    	}
    	public boolean isTabletDevice(Context context) {
    	    boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
    	    boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
    	    return (xlarge || large);
    	}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.activity_menu_2, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		 switch (item.getItemId()) {
		 case android.R.id.home :  //up to 1st screen: Camera
			 NavUtils.navigateUpTo(this, new Intent(this, New_FirstScreen.class));
			 return true;	
		 case R.id.menu_accept:
			 Intent int2=new Intent(this,EffectScreen.class);
			 int2.putExtra("from_screen", 2);
			 int2.putExtra("image", this.burstmode_images.get(this.selected_image_index));
			 startActivity(int2);
			 return true;
		 }
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	    	NavUtils.navigateUpTo(this, new Intent(this, New_FirstScreen.class));
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
}


